//
//  OrderDetailTableViewController.m
//  uplife
//
//  Created by ZhangCheng on 2019/1/8.
//  Copyright © 2019年 ZhangCheng. All rights reserved.
//

#import "OrderDetailTableViewController.h"
#import "UIImageView+WebCache.h"

@interface OrderDetailTableViewController ()
@property(nonatomic,assign)NSInteger currentNumber;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalPrice;
@property (weak, nonatomic) IBOutlet UITextField *usernameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *addrTF;
@property (weak, nonatomic) IBOutlet UITextField *mailTF;
@property (weak, nonatomic) IBOutlet UITextField *liuYanTF;
@property (nonatomic,weak)IBOutlet UIStepper *stepper;
@end

@implementation OrderDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentNumber = 1;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",JSXF_HOST_FILE,self.dataDic[@"pic"]]]];
    self.nameLabel.text = [NSString stringWithFormat:@"%@",self.dataDic[@"name"]];
    self.totalPrice.text = FORMAT_PRICE([NSNumber numberWithFloat:( self.currentNumber * ((NSNumber *)self.dataDic[@"currentPrice"]).floatValue)]);
    self.stepper.maximumValue = ((NSNumber*)self.dataDic[@"stockQuantity"]).doubleValue;
}

- (IBAction)addOrSubject:(UIStepper*)sender {
    self.currentNumber = sender.value;
    self.numberLabel.text = [NSString stringWithFormat:@"购买数量：%@",@(self.currentNumber)];
    self.totalPrice.text = FORMAT_PRICE([NSNumber numberWithFloat:( self.currentNumber * ((NSNumber *)self.dataDic[@"currentPrice"]).floatValue)]);
}

-(BOOL)canSubmit{
    BOOL result = YES;
    NSString *username = [self.usernameTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *phone = [self.phoneTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *addr = [self.addrTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *mail = [self.mailTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (username.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入收货人姓名"];
        result = NO;
    }
    if (phone.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入收货人电话"];
        result = NO;
    }
    if ([[ZCTool sharedInstance]validateMobile:phone]==NO) {
        [SVProgressHUD showErrorWithStatus:@"电话格式有误"];
        result = NO;
    }
    if (addr.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入收货人地址"];
        result = NO;
    }
    if (mail.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入快递费用"];
        result = NO;
    }
    return result;
}

- (IBAction)submitAction:(id)sender {
    if ([self canSubmit]) {
        NSString *username = [self.usernameTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSString *phone = [self.phoneTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSString *addr = [self.addrTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSString *mail = [self.mailTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSString *liuYan = [self.liuYanTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        NSString *urlString = [NSString stringWithFormat:@"%@/order/save.do",JSXF_HOST];
        NSDictionary *params = @{
                                 @"productId":self.dataDic[@"id"],
                                 @"price":self.dataDic[@"currentPrice"],
                                 @"count":@(self.currentNumber),
                                 @"customerMobile":phone,
                                 @"receivingAddr":addr,
                                 @"receivingName":username,
                                 @"buyerMsg":[NSString stringWithFormat:@"%@",liuYan],
                                 @"type":@"1",
                                 @"expressFee":@(0)
                                 };
        [SVProgressHUD show];
        [ZCWebServiceHelper requestWithMethod:POST andUrlStr:urlString andParameters:params andFinished:^(id response, NSURLSessionDataTask *dataTask) {
            [SVProgressHUD showSuccessWithStatus:response[@"message"]];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
